package com.hit.aircraftwar.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.hit.aircraftwar.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Objects;

import okhttp3.Call;

public class SignActivity extends AppCompatActivity {

    private static final String TAG ="SignActivity";

    private EditText et_account;
    private EditText et_password;
    private EditText et_confirm;
    private String url = "http://426k04070x.qicp.vip/user/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Objects.requireNonNull(getSupportActionBar()).hide();


        et_account = findViewById(R.id.et_account);
        et_password = findViewById(R.id.et_password);
        et_confirm = findViewById(R.id.et_password_Confirm);
        Button btn_register = findViewById(R.id.btn_register);
        btn_register.setOnClickListener(view -> {
            //发送注册请求，应该不需要新建线程
            if(String.valueOf(et_password.getText()).equals(String.valueOf(et_confirm.getText()))){
                OkHttpUtils
                    .post()
                    .url(url+"signUp")
                    .addParams("id", String.valueOf(et_account.getText()))
                    .addParams("password", String.valueOf(et_password.getText()))
                    .addParams("email","707079096@qq.com")
                    .addParams("point","2000")
                    .build()
                    .execute(new StringCallback() {

                        @Override
                        public void onError(Call call, Exception e, int id) {
                            e.printStackTrace();
                            Log.i(TAG,"连接失败");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            if (response.equals("true")){
                                Intent intent = new Intent(SignActivity.this, LoginActivity.class);
                                startActivity(intent);
                                Log.i(TAG,"注册成功");
                            }else {
                                Log.i(TAG,"注册失败");
                            }
                        }
                    });
            } else {
                showDialog();
                Log.i(TAG,"两次密码不一致");
            }
        });

    }

    private void showDialog(){
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("");
        normalDialog.setMessage("两次密码不一致！");
        normalDialog.setPositiveButton("重新输入",
                (dialog, which) -> {
                    et_password.setText("");
                    et_confirm.setText("");
                });
        // 显示
        normalDialog.show();
    }
}
